<?php
//
// 分享家管理
//
class Controller_Admin_ShareUser extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type  = 'shareuser';
        $this->_no_buttons = TRUE;
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=',  1),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加分享家
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改分享家
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    // 收入流水
    public function action_income ($shareUid)
    {
        $page = Arr::get($this->request->query(), 'page', 1);
        $page = max(1, $page);
        $limit  = 20;
        $offset = ($page - 1) * $limit;

        $list  = App_Share_User::incomeList($shareUid, 0, $offset, $limit);
        $count = App_Share_User::incomeCount($shareUid, 0);
        
        $this->template->set('records', $list);
        $this->template->set('pagination', $this->pagination($count, $limit, 'querying', 'page'));
        $this->template->set_filename('smarty:admin/share/income_list'); 
    }

	// 详情
	//$nid int
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->RMBFen2Yuan($data['money']);
        $this->RMBFen2Yuan($data['moneyIncome']);
        $this->RMBFen2Yuan($data['money2Cash']);
        $this->RMBFen2Yuan($data['money2Coin']);
        $data['created'] = date("Y-m-d H:i:s", $data['created']);

        $this->_form_data = $data;

        parent::action_detail($nid);
    }

	// 删除分享家
    public function action_delete ($nid)
    {
        App_Deliver::remove($nid);
        $this->to_url('list');
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        //
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $this->RMBFen2Yuan($rs['money']);
            $this->RMBFen2Yuan($rs['moneyIncome']);
            $this->RMBFen2Yuan($rs['money2Cash']);
            $this->RMBFen2Yuan($rs['money2Coin']);
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
    }
}
